package logic

import (
	"context"
	"errors"
	"product_server/dao/dao_redis"
	product "product_server/proto"
)

func (p *ProductServer) UpdateCarProduct(ctx context.Context, req *product.UpdateCarProductRequest) (*product.UpdateCarProductResponse, error) {
	//购物车商品是否存在
	if !dao_redis.GetCatExists(req.Uid, req.ProductId) {
		return nil, errors.New("购物车商品不存在")
	}
	//判断添加还是移除
	if req.Govern == 1 {
		//添加
		err := dao_redis.UpdateCarProductNumAdd(req.Uid, req.ProductId, req.Num)
		if err != nil {
			return nil, errors.New("购物车商品数量增加失败")
		}
	} else {
		//减少
		err := dao_redis.UpdateCarProductNumDel(req.Uid, req.ProductId, req.Num)
		if err != nil {
			return nil, errors.New("购物车商品数量减少失败")
		}
	}
	return &product.UpdateCarProductResponse{Success: true}, nil
}
